<?php 
	include '../init.php';
	$model=new Model('goods');
	$model1=new Model('goods_img');
	$userid=$_SESSION['admin']['id'];

	$a=$_GET['a'];
	switch ($a) 
	{
		case 'add':
			//判断是否为空
		
			$_POST['pic']=$_FILES['pic']['name'];
			//var_dump($_POST);
			if (!empty($_POST)) 
			{
				foreach ($_POST as $key => $value) 
				{
					if (empty($value)) 
					{
						error('表单没有填写完整！');
						return false;
					}
				}
			}
			//exit;

			//判断图片是否上传
			if ($_FILES['pic']['error']>0) 
			{
				error('图片上传失败！');
				return false;
			}

			//接受数据
			$data['name']=$_POST['name'];
			$data['price']=$_POST['price'];
			$data['cate_id']=$_POST['cate_id'];
			$data['user_id']=$userid;
			$data['stock']=$_POST['stock'];
			$data['description']=$_POST['description'];
			$data['addtime']=time();

			//插入商品表返回商品的id
			$goods_id=$model->insert($data);

			if ($goods_id) 
			{
				//上传图片，返回图片的name
				$filename=Upload('pic',PATH.'/public/uploads/');
				//写一个数组
				$dat['goods_id']=$goods_id;
				$dat['name']=$filename;
				$dat['is_cover']=1;
				//插入图片 返回图片的id
				$res=$model1->insert($dat);
				if ($res) 
				{
					success('商品添加成功！',ADMIN_URL.'/goods/index.php',1);
					return false;
				}
			}
			break;
		
			case 'new':
				$id=$_GET['id'];
				//接受当前页显示
				$p=$_GET['p'];

				$sql="select `is_new` from `goods` where id=$id";
				$res=$model->find($sql)[0]['is_new'];
				$res=$res?0:1;
				$data['is_new']=$res;
				$where['id']=$id;
				$result=$model->update($data,$where);

				if ($result) 
				{
					header('location:'.ADMIN_URL.'/goods/index.php?p='.$p);
					return false;
				}
				break;

			case 'bast':
				$id=$_GET['id'];
				//接受当前页显示
				$p=$_GET['p'];

				$sql="select `is_bast` from `goods` where id=$id";
				$res=$model->find($sql)[0]['is_bast'];
				$res=$res?0:1;
				$data['is_bast']=$res;
				$where['id']=$id;
				$result=$model->update($data,$where);

				if ($result) 
				{
					header('location:'.ADMIN_URL.'/goods/index.php?p='.$p);
					return false;
				}
				break;

			case 'hot':
				$id=$_GET['id'];
				//接受当前页显示
				$p=$_GET['p'];

				$sql="select `is_hot` from `goods` where id=$id";
				$res=$model->find($sql)[0]['is_hot'];
				$res=$res?0:1;
				$data['is_hot']=$res;
				$where['id']=$id;
				$result=$model->update($data,$where);

				if ($result) 
				{
					header('location:'.ADMIN_URL.'/goods/index.php?p='.$p);
					return false;
				}
				break;

		case 'addimg':
			//商品的id
			$id=$_POST['id'];
			//判断是否上传
			if ($_FILES['pic']['error']>0) 
			{
				error('上传图片失败');
				return false;
			}

			//上传图片
			$filename=Upload('pic',PATH.'/public/uploads');

			//接收数据库
			$data['name']=$filename;
			$data['is_cover']=$_POST['is_cover'];
			$data['goods_id']=$id;
			//插入之前判断是否封面
			if ($data['is_cover']) 
			{
				//更改封面或添加封面
				$dat['is_cover']=0;
				$where['goods_id']=$id;
				$result=$model1->update($dat,$where);
				$res=$model1->insert($data);
				if ($res) 
				{
					success('添加图片成功',ADMIN_URL.'/goods/detail.php?id='.$id,1);
				}
			}else{
				//不添加封面，添加封面下的图片
				$res=$model1->insert($data);
				if ($res) 
				{
					success('添加图片成功',ADMIN_URL.'/goods/detail.php?id='.$id,1);
				}
			}
			break;

		case 'delete':
			$id=$_GET['id'];
			//查出图片名称
			$sql="select `name` from `goods_img` where goods_id=$id";
			$list=$model1->find($sql);

			//删除字段
			$res=$model->delete($id);
			if ($res) 
			{
				//删除图片
				$sql="delete from `goods_img` where goods_id=$id";
				$result=$model1->find($sql);

				foreach ($list as $key => $value) 
				{	//删除路径下的图片
					unlink(PATH.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/'.$value['name']);
					unlink(PATH.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/50_'.$value['name']);
					unlink(PATH.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/80_'.$value['name']);
					unlink(PATH.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/200_'.$value['name']);
					unlink(PATH.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/500_'.$value['name']);
				}
				success('删除成功',ADMIN_URL.'/goods/index.php',1);
					return false;
			}
			break;
	}
?>